home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / c / expat-dev.lha / expat-1.95.5 / examples / outline.c < prev   
C/C++ Source or Header  |  2002-09-09  |  3KB  |  130 lines

  1. /*****************************************************************
  2.  * outline.c
  3.  *
  4.  * Copyright 1999, Clark Cooper
  5.  * All rights reserved.
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the license contained in the
  9.  * COPYING file that comes with the expat distribution.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  14.  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  15.  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  16.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  17.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18.  *
  19.  * Read an XML document from standard input and print an element
  20.  * outline on standard output.
  21.  */
  22.  
  23.  
  24. #include <stdio.h>
  25. #ifndef AMIGA
  26. #include <expat.h>
  27. #else
  28. #include <exec/types.h>
  29. #include <exec/memory.h>
  30. #include <expat/expat.h>
  31. #include <proto/exec.h>
  32. #include <proto/expat.h>
  33.  
  34. struct Library *ExpatBase = NULL;
  35.  
  36. #endif
  37.  
  38. #define BUFFSIZE    8192
  39.  
  40. char Buff[BUFFSIZE];
  41.  
  42. int Depth;
  43.  
  44.  
  45. static void
  46. start(void *data, const char *el, const char **attr)
  47. {
  48.   int i;
  49.  
  50.   for (i = 0; i < Depth; i++)
  51.     printf("  ");
  52.  
  53.   printf("%s", el);
  54.  
  55.   for (i = 0; attr[i]; i += 2) {
  56.     printf(" %s='%s'", attr[i], attr[i + 1]);
  57.   }
  58.  
  59.   printf("\n");
  60.   Depth++;
  61. }
  62.  
  63. static void
  64. end(void *data, const char *el)
  65. {
  66.   Depth--;
  67. }
  68.  
  69. int
  70. main(int argc, char *argv[])
  71. {
  72.   XML_Parser p;
  73. #ifdef AMIGA
  74.   ExpatBase = (APTR) OpenLibrary("expat.library", 0);
  75.   if(!ExpatBase)
  76.   {
  77.     puts("\nCouldn't open expat.library\n");
  78.     exit(20);
  79.   }
  80. #endif
  81.   
  82.   p = XML_ParserCreate(NULL);
  83.   if (! p) {
  84.     fprintf(stderr, "Couldn't allocate memory for parser\n");
  85. #ifdef AMIGA  
  86.       CloseLibrary((APTR) ExpatBase);;
  87. #endif
  88.     exit(-1);
  89.   }
  90.  
  91.   XML_SetElementHandler(p, start, end);
  92.  
  93.   for (;;) {
  94.     int done;
  95.     int len;
  96.  
  97.     len = fread(Buff, 1, BUFFSIZE, stdin);
  98.     if (ferror(stdin)) {
  99.       fprintf(stderr, "Read error\n");
  100. #ifdef AMIGA  
  101.       CloseLibrary((APTR) ExpatBase);;
  102. #endif
  103.       exit(-1);
  104.     }
  105.     done = feof(stdin);
  106.  
  107.     if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR) {
  108.       fprintf(stderr, "Parse error at line %d:\n%s\n",
  109.           XML_GetCurrentLineNumber(p),
  110.           XML_ErrorString(XML_GetErrorCode(p)));
  111. #ifdef AMIGA  
  112.       CloseLibrary((APTR) ExpatBase);;
  113. #endif
  114.       exit(-1);
  115.     }
  116.  
  117.     if (done)
  118.       break;
  119.   }
  120.   
  121. #ifdef AMIGA  
  122.   CloseLibrary((APTR) ExpatBase);
  123.   exit(0);
  124. #else
  125.   return 0;
  126. #endif
  127.   
  128. }
  129.  
  130.